<template>
    <div class="check-plan-page seeDisabled">
        <!-- 
        <router-link to="/SparePlan" style="float: right">
            <el-button type="danger" icon="close" size="mini" class="bolder outsideBtnBS"></el-button>
        </router-link>
        -->
        <el-form :inline="true" ref="form" :model="checkForm" style="margin-bottom:20px;border-bottom:1px solid #ccc;">
            <el-form-item label="计划编号:" class="selectMB10">
                <el-input size="mini" disabled placeholder="" v-model="checkForm.planCode"></el-input>
            </el-form-item>
            <el-form-item label="制定人:" class="selectMB10">
                <el-input size="mini" disabled v-model="checkForm.creatorName"></el-input>
            </el-form-item>
            <el-form-item label="制定时间:" class="selectMB10">
                <el-input size="mini" disabled v-model="checkForm.createTime"></el-input>
            </el-form-item>
            <el-form-item label="法人名称:" class="selectMB10">
                <el-select size="mini" disabled v-if="legalPersonLists.length>1" v-model="checkForm.legalPersonName"placeholder="请选择">
                    <!--   @change="selectLegalPersonName" 
                    <el-option v-for="legalPerson in legalPersonLists" :label="legalPerson.legalPersonName" :value="legalPerson.legalPersonName">
                    </el-option>
                    -->
                </el-select>
                <el-input size="mini" disabled v-else v-model="checkForm.legalPersonName" placeholder=""></el-input>
            </el-form-item>
            <el-form-item label="计划来源:" class="selectMB10">
                <el-select size="mini" disabled v-model="checkForm.source" placeholder="请选择">
                    <!-- 
                    <el-option v-for="source in sourceLists" :label="source.label" :value="source.value">
                    </el-option>
                    -->
                </el-select>
            </el-form-item>
            <el-form-item label="计划币别:" class="selectMB10">
                <el-select size="mini" disabled v-model="checkForm.currencyCode" placeholder="请选择">
                    <!-- 
                    <el-option v-for="currencyCode in currencyCodeLists" :label="currencyCode.label" :value="currencyCode.value">
                    </el-option>
                    -->
                </el-select>
            </el-form-item>
            <el-form-item label="计划总件数:" class="selectMB10">
                <el-input size="mini" disabled v-model="checkForm.totalCount"></el-input>
            </el-form-item>
            <el-form-item label="计划总金额:" class="selectMB10">
                <el-input size="mini" disabled v-model="checkForm.total"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button v-if="checkForm.status==2 && powerFlag2" type="danger" size="mini" class="bolder outsideBtnBS" @click="handleReview">审核</el-button>
                <el-button v-if="checkForm.status==3 && powerFlag3" type="danger" size="mini" class="bolder outsideBtnBS" @click="handleApprove">审批</el-button>
            </el-form-item>
             
        </el-form>
        <el-form :inline="true" ref="form1" :model="checkDetailForm">
            <el-form-item label="备件编码">
                <el-input size="mini" v-model="checkDetailForm.materialCode" placeholder=""></el-input>
            </el-form-item>
            <el-form-item label="备件分类:">
                <el-select v-model="checkDetailForm.materialSubclass" placeholder="" size="mini" clearable>
                    <el-option v-for="(materialSubclass,index) in materialSubclassLists" :key="index" :label="materialSubclass" :value="materialSubclass">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="danger" size="mini" class="bolder outsideBtnBS selectAll" :disabled="selectDisabled" @click="checkTheDetails()">查询</el-button>
                <!--新增按钮 
                <el-button type="danger" disabled size="mini" class="bolder outsideBtnBS">新增</el-button>-->
            </el-form-item>
        </el-form>

        <!--表格-->
        <el-table :data="detailsData" border style="width: 100%">
            <el-table-column type="index" header-align="center" align="center" label="序"  width="40"></el-table-column>
            <el-table-column prop="materialSubclass" label="分类"  width="60" header-align="center" show-overflow-tooltip> </el-table-column>
            <el-table-column prop="materialCode" label="备件编码"   header-align="center" show-overflow-tooltip> </el-table-column>
            <el-table-column prop="materialName" label="备件名称"  header-align="center" show-overflow-tooltip> </el-table-column>
            <el-table-column prop="standard" label="规格"  width="60" header-align="center" show-overflow-tooltip> </el-table-column>
            <el-table-column prop="unit" label="计量单位"  width="80" header-align="center" show-overflow-tooltip> </el-table-column>
            <el-table-column prop="quantity" label="计划数量"  width="80" header-align="center"> </el-table-column>
            <el-table-column prop="currencyCodeWord" label="币别"  width="60" header-align="center" show-overflow-tooltip> </el-table-column>
            <el-table-column prop="price" label="单价"  width="60" header-align="center" align="right"> </el-table-column>
            <el-table-column prop="amount" label="金额"  width="80" header-align="center" align="right"> </el-table-column>
            <el-table-column prop="deliveryTimeExpectation" label="期望到货时间"  header-align="center" show-overflow-tooltip> </el-table-column>
            <el-table-column prop="remark" label="备注"  header-align="center" show-overflow-tooltip> </el-table-column>
        </el-table>

        <!--分页
        <el-pagination v-if="isPaging" @size-change="handleSizeChange" @current-change="handleCurrentChange"
                       :current-page.sync="currentPage3" :current-page="pageIndex"
                       :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
                       :page-count="totalPage" :total="totalNumber">
        </el-pagination>
        -->
        <!-- 点击审核 审核弹窗 -->
        <el-dialog title="审核提示信息" :visible.sync="handleReviewVisible" size="small" :lock-scroll="false">
            <el-form :model="reviewForm">
                <h2>是否审核通过当前单据？</h2>
                <el-form-item label="审核意见" :label-width="formLabelWidth">
                    <el-input type="textarea"  :rows="3" placeholder="请输入内容" v-model="reviewForm.textarea">
                    </el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="reviewPlan(true)">审核通过</el-button>
                <el-button @click="reviewPlan(false)">审核不通过</el-button>
            </div>
        </el-dialog>
        <!-- 点击审批 审批弹窗 -->
        <el-dialog id="approvalMask" title="审批提示信息" :visible.sync="handleApprovalVisible" size="small" :lock-scroll="false">
            <el-form :model="approvalForm">
                <h2>是否审批通过当前单据？</h2>
                <el-form-item label="审批意见" :label-width="formLabelWidth">
                    <el-input type="textarea"  :rows="3" placeholder="请输入内容" v-model="approvalForm.textarea">
                    </el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="approvePlan(true)">审批通过</el-button>
                <el-button @click="approvePlan(false)">审批不通过</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        data(){
            return{
                labelWidth:"80px",
                checkForm:{
                    planCode:'',
                    planId:'',
                    legalPersonName:'',
                    legalPersonId:'',
                    creatorName:'',
                    createTime:'',
                    totalCount:'',
                    total:'',
                    source:'',
                    currencyCode:''
                },
                legalPersonLists:[],   //法人名称下拉列表中的值
                sourceLists:[],   //计划来源下拉列表的值
                currencyCodeLists:[],   //计划币别下拉列表的值
                materialSubclassLists:[],   //计划明细 查询条件 备件状态
                checkDetailForm:{
                    materialCode:'',
                    materialSubclass:''
                },
                // isPaging:false,
                pageIndex: 1,    //当前页（必传）
                pageSize: 10,   //每页多少条
                totalNumber: 20,
                totalPage: 2,
                currentPage3:3,
                detailsData:[],

                handleReviewVisible:false,    //审核弹窗
                reviewForm:{      //审核内容
                    textarea:""
                },
                handleApprovalVisible:false,    // 审批弹窗
                approvalForm:{      //审批内容
                    textarea:""
                },
                formLabelWidth:"90",
                //查询禁用
                selectDisabled:false,
                //权限
                powerFlag2:false,
                powerFlag3:false
            }
        },
        computed:{
            powerData(){
                return this.$store.state.powerData;
            }
        },
        watch:{
            powerData(){
                var _this=this;
                for(var i=0;i<_this.powerData.length;i++){
                    if(_this.powerData[i]=="ROLE_security_materialPlan_review"){
                        //备件计划审核权限
                        _this.powerFlag2=true;
                    }
                    if(_this.powerData[i]=="ROLE_security_materialPlan_approve"){
                        //备件计划审批权限
                        _this.powerFlag3=true;
                    }
                }
            }
        },
        mounted:function(){
            var _this=this;
            _this.$store.commit('currentActive', 'CheckPlan');
            // //ajax前后查询按钮禁用
            // $(".selectAll").ajaxStart(function(){
            //     _this.selectDisabled=true;
            // });
            // $(".selectAll").ajaxStop(function(){
            //     _this.selectDisabled=false;
            // });
            //判断权限
            for(var i=0;i<_this.powerData.length;i++){
                if(_this.powerData[i]=="ROLE_security_materialPlan_review"){
                    //备件计划审核权限
                    _this.powerFlag2=true;
                }
                if(_this.powerData[i]=="ROLE_security_materialPlan_approve"){
                    //备件计划审批权限
                    _this.powerFlag3=true;
                }
            }
            _this.checkForm.planId=_this.$route.query.planId;        //?
            _this.checkForm.status=_this.$route.query.status;
            _this.planSCDropDownValue();   //计划明细  计划来源和计划明细 下拉列表
            _this.legalPersonDropDownValue(); //计划明细  法人名称 下拉列表
            //页面一打开  获取当前计划的计划明细
            _this.planItemDetaisFrom();
            // 计划明细_this.pageIndex,_this.pageSize
            _this.checkTheDetails();
        },
        methods:{
            // 查询条件 法人名称 下拉列表的值
            legalPersonDropDownValue(){  //法人名称下拉列表值
                var _this = this;
                $.ajax({
                    type:'GET',
                    url:_this.procurement_service+"legalPerson/ofCurrentUser",
                    cache:false,
                    success:function(data){
                        if(data.success){
                            _this.legalPersonLists = data.data;
                        }else{
                            _this.common.errorTip(data.errorMessage);
                        }
                    }
                })
            },
            // // 下拉列表选择法人名称
            // selectLegalPersonName(){
            //     var _this=this;
            //     for(var i=0;i<_this.legalPersonLists.length;i++){
            //         if(_this.checkForm.legalPersonName==_this.legalPersonLists[i].legalPersonName){
            //             _this.checkForm.legalPersonId=_this.legalPersonLists[i].id;
            //         }
            //     }
            // },
            //  计划明细  计划来源和计划明细 下拉列表
            planSCDropDownValue(){
                var _this=this;
                var tmp = ["source","currencyCode"];
                $.ajax({
                    type:'POST',
                    url:_this.procurement_service+'procurement/getPullDownList',
                    cache:false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8",     //*****
                    success:function(data){
                        if(data.success){
                            // console.log(data.data);
                            _this.sourceLists = data.data.source;
                            _this.currencyCodeLists = data.data.currencyCode;
                        }else{
                            _this.common.errorTip(data.errorMessage);
                        }
                    }
                })
            },
            //页面一打开  获取当前计划的计划明细
            planItemDetaisFrom(){
                var _this=this;
                var tmp = {planId:_this.checkForm.planId,pageIndex:_this.pageIndex,pageSize:_this.pageSize};
                $.ajax({
                    type: 'POST',
                    url: _this.procurement_service+"plan/queryPlan",
                    cache: false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8",     //*****
                    success: function (data) {
                        if (data.success) {
                            _this.loading=false;
                            _this.isPaging=true;
                            // console.log(data.data);
                            data.data.dataList[0].total=data.data.dataList[0].total.toFixed(2);
                            _this.checkForm=data.data.dataList[0];  
                            _this.checkForm.legalPersonId=data.data.dataList[0].legalPersonId;
                            _this.checkForm.source=data.data.dataList[0].source;
                            for(var i=0;i<_this.legalPersonLists.length;i++){
                                if(_this.checkForm.legalPersonId==_this.legalPersonLists[i].id){
                                    _this.checkForm.legalPersonName=_this.legalPersonLists[i].legalPersonName;
                                }
                            }
                            for(var i=0;i<_this.sourceLists.length;i++){
                                if(_this.checkForm.source==_this.sourceLists[i].value){
                                    _this.checkForm.source=_this.sourceLists[i].label;
                                }
                            }
                            for(var i=0;i<_this.currencyCodeLists.length;i++){
                                if(_this.checkForm.currencyCode==_this.currencyCodeLists[i].value){
                                    _this.checkForm.currencyCode=_this.currencyCodeLists[i].label;
                                }
                            }
                            _this.selectMaterialSubclassDropDownValue();  // 计划明细弹窗 备件分类下拉列表的值
                        }else
                            // console.log(data.errorMessage);                             
                            _this.common.errorTip(data.errorMessage);
                    }
                });
            },
            // 计划明细 查询条件 备件分类下拉列表
            selectMaterialSubclassDropDownValue(){
                var _this = this;
                $.ajax({
                    type:'GET',
                    url:_this.procurement_service+"plan/selectMaterialSubclass?planId="+_this.checkForm.planId,
                    cache:false,
                    success:function(data){
                        if(data.success){
                            _this.materialSubclassLists = data.data;
                            // console.log(_this.materialSubclassLists);
                        }else{
                            _this.common.errorTip(data.errorMessage);
                        }
                    }
                })
            },
            // 计划明细 pageIndex,pageSize ,pageIndex:pageIndex,pageSize:pageSize
            checkTheDetails(){
                var _this=this;
                _this.checkTheDetailsVisible=true;
                var tmp = {planId:_this.checkForm.planId,materialCode:_this.checkDetailForm.materialCode, materialSubclass:_this.checkDetailForm.materialSubclass};
                _this.selectDisabled=true;
                $.ajax({
                    type: 'POST',
                    url: _this.procurement_service+"plan/queryPlanItem",
                    cache: false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8",     //*****
                    success: function (data) {
                        if (data.success) {
                            // _this.loading=false;
                            // console.log(data.data);
                            var arr=data.data.dataList;
                            //金额保留两位
                            for(var i=0;i<arr.length;i++){
                                arr[i].price=arr[i].price.toFixed(2);
                                arr[i].amount=arr[i].amount.toFixed(2);
                            }
                            _this.detailsData=arr;
                            // _this.totalNumber=data.data.totalNumber;    //总条数
                            // _this.totalPage=data.totalPage;    //总页数 
                        }else{
                            _this.common.errorTip(data.errorMessage);
                        }                            
                        _this.selectDisabled=false;    
                    }
                });

            },
            // //   分页
            // handleSizeChange(val) {
            //     // console.log(`每页 ${val} 条`);
            //     var _this = this;
            //     _this.pageSize=val;
            //     _this.checkTheDetails(_this.pageIndex, _this.pageSize);
            // },
            // //点击页码分页查询点击事件
            // handleCurrentChange(val) {
            //     // console.log(`当前页: ${val}`);
            //     var _this = this;
            //     _this.pageIndex=val;
            //     _this.checkTheDetails(_this.pageIndex, _this.pageSize);
            // },
            // 计划 审核弹窗
            handleReview(){
                var _this=this;
                _this.handleReviewVisible=true;
            },
            // 计划 审核通过
            reviewPlan(reviewStatus){
                var _this = this;
                var tmp = {planId:_this.checkForm.planId,reviewStatus:reviewStatus};
                $.ajax({
                    type: 'POST',
                    url: _this.procurement_service+"plan/review",
                    cache: false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8", 
                    success: function (data) {
                        if (data.success) {
                            // console.log(data.data);
                            _this.common.successTip("操作成功！");
                            _this.handleReviewVisible=false;
                            _this.$router.push("/SparePlan");
                        }else
                            // console.log(data.errorMessage);  
                            _this.common.errorTip(data.errorMessage);
                    }
                });
            },
            // 计划 审批弹窗
            handleApprove(){
                var _this=this;
                _this.handleApprovalVisible=true;
            },
            // 计划 审批
            approvePlan(approveStatus){
                var _this = this;
                var tmp = {planId:_this.checkForm.planId,approveStatus:approveStatus};
                $.ajax({
                    type: 'POST',
                    url: _this.procurement_service+"plan/approve",
                    cache: false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8", 
                    success: function (data) {
                        if (data.success) {
                            // console.log(data.data);
                            _this.common.successTip("操作成功！");
                            _this.handleApprovalVisible=false;
                            _this.$router.push("/SparePlan");
                        }else
                            // console.log(data.errorMessage);  
                            _this.common.errorTip(data.errorMessage);
                    }
                });
            },
            //  // ===================================
            // successTip(p) {     //成功时的提示信息
            //     this.$notify({
            //         title: '成功',
            //         message: p,
            //         type: 'success'
            //     });
            // },
            // errorTip(p) {     //错误提示
            //     this.$notify.error({
            //         title: '错误',
            //         message: p
            //     });
            // }
          
        }
    }
</script>

<style>

</style>